<template>
  <div class="login">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;" class="login-button">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: "test2",
      password: "test2"
    };
  },
  methods: {
    //获取数据
    onSubmit() {
      this.$http
        .post("/admin/login", {
          //传参
          username: this.username,
          password: this.password
        })
        .then(res => {
          console.log(res);
          //判断登录状态是否成功
          if (res.data.status === 200) {
            alert("登录成功");
            //跳转页面
            this.$router.push("/home");
            //存储token里
            localStorage.setItem('token',res.data.body.token)
          }
        });
    }
  }
};
</script>

<style>
.login-button .van-button {
  background: rgb(26, 219, 26);
  border: none;
}
</style>